package Viewer;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import DataModel.Feature;
import DataModel.FeatureHolder;
import DataModel.LociHolder;

/**
 * Panel of the name and position search.<br>
 * It enables the search of {@link Feature}s by its name or position
 * 
 * @author Stefan Lorenz<br>
 * <br>
 *         29.11.2012
 */
@SuppressWarnings("serial")
public class NamePositionSearch extends JPanel {

	/**
	 * Viewer of the GeneScapes session
	 */
	private Viewer viewer;
	/**
	 * Combobos where all feature names are listed
	 */
	private AutoCompleteBox autoCompleteBox;

	/**
	 * Constructor of the name and position search panel
	 * 
	 * @param viewer
	 *            viewer of the GeneScapes session
	 */
	public NamePositionSearch(final Viewer viewer) {
		this.viewer = viewer;

		TitledBorder titledBorder = BorderFactory.createTitledBorder(null, "Position/ID Search:", TitledBorder.DEFAULT_JUSTIFICATION, TitledBorder.RIGHT, new Font("Arial", Font.BOLD, 14), Color.BLACK);
		this.setBorder(titledBorder);

		final JTextField jumpTo = new JTextField();

		jumpTo.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (Integer.valueOf(jumpTo.getText()) <= viewer.getSizeOfReference()) {
						int diff = Integer.valueOf(jumpTo.getText()) - viewer.getPositionOfBrowser(1);
						for (int i = 0; i < viewer.getPositionOfTrack().length; i++) {
							viewer.setPositionOfBrowser(i, viewer.getPositionOfBrowser(i) + diff + -(viewer.getRangeOfBrowser() / 2));
						}
					}
				} catch (NumberFormatException e) {
					JOptionPane.showMessageDialog(viewer, "Not a numeric entry!", "INFORMATION", JOptionPane.INFORMATION_MESSAGE);
				}
			}
		});
		// set track index of auto complete search box to standard value
		updateSearchBox(1);
		jumpTo.setMinimumSize(this.autoCompleteBox.getPreferredSize());
		jumpTo.setPreferredSize(this.autoCompleteBox.getPreferredSize());
		this.autoCompleteBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					int diff = ((LociHolder) viewer.getLoadedTracks()[1]).getList().get(autoCompleteBox.getSelectedIndex()).getStart() - viewer.getPositionOfBrowser(1);
					for (int i = 0; i < viewer.getPositionOfTrack().length; i++) {
						viewer.setPositionOfBrowser(i, viewer.getPositionOfBrowser(i) + diff + -(viewer.getRangeOfBrowser() / 2));
					}
				} catch (Exception e) {
					System.err.println("can't go to position of " + autoCompleteBox.getSelectedItem().toString());
				}
			}
		});

		this.setLayout(new GridBagLayout());
		GridBagConstraints gc = new GridBagConstraints();
		gc.gridx = 0;
		gc.gridy = 0;
		JLabel pos = new JLabel("Position     ");
		pos.setForeground(Color.BLACK);
		this.add(pos, gc);
		gc.gridx = 0;
		gc.gridy = 1;
		JLabel id = new JLabel("ID     ");
		id.setForeground(Color.BLACK);
		this.add(id, gc);
		gc.gridx = 1;
		gc.gridy = 0;
		this.add(jumpTo, gc);
		gc.gridx = 1;
		gc.gridy = 1;
		this.add(this.autoCompleteBox, gc);
	}

	/**
	 * Updates the search box if another track is selected
	 * 
	 * @param index
	 *            index of the track to show in combobox
	 */
	public void updateSearchBox(int index) {
		ArrayList<String> seachList = new ArrayList<String>();

		if (viewer.getLoadedTracks()[index].getTypeOfFile().contains("embl") || viewer.getLoadedTracks()[index].getTypeOfFile().contains("gbk")) {
			LociHolder genome = (LociHolder) viewer.getLoadedTracks()[index];
			for (int i = 0; i < genome.getList().size(); i++) {
				seachList.add(genome.getList().get(i).getName());
			}
		} else if (!viewer.getLoadedTracks()[index].getTypeOfFile().contains("wig") && !viewer.getLoadedTracks()[index].getTypeOfFile().contains("fas")) {
			FeatureHolder genome = (FeatureHolder) viewer.getLoadedTracks()[index];
			for (int i = 0; i < genome.size(); i++) {
				seachList.add(genome.get(i).getName());
			}
		}
		this.autoCompleteBox = new AutoCompleteBox(seachList);
		this.autoCompleteBox.revalidate();
		this.autoCompleteBox.repaint();
	}
}
